package product

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/flipped-aurora/gin-vue-admin/server/model/product"
	productReq "github.com/flipped-aurora/gin-vue-admin/server/model/product/request"
	"github.com/flipped-aurora/gin-vue-admin/server/service"
	"github.com/flipped-aurora/gin-vue-admin/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type BsProductInfoApi struct {
}

var bsProductInfoService = service.ServiceGroupApp.ProductServiceGroup.BsProductInfoService

// CreateBsProductInfo 创建二手商品
// @Tags BsProductInfo
// @Summary 创建二手商品
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body product.BsProductInfo true "创建二手商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /bsProductInfo/createBsProductInfo [post]
func (bsProductInfoApi *BsProductInfoApi) CreateBsProductInfo(c *gin.Context) {
	var bsProductInfo product.BsProductInfo
	err := c.ShouldBindJSON(&bsProductInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	bsProductInfo.CreatedBy = utils.GetUserID(c)

	if err := bsProductInfoService.CreateBsProductInfo(&bsProductInfo); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteBsProductInfo 删除二手商品
// @Tags BsProductInfo
// @Summary 删除二手商品
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body product.BsProductInfo true "删除二手商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /bsProductInfo/deleteBsProductInfo [delete]
func (bsProductInfoApi *BsProductInfoApi) DeleteBsProductInfo(c *gin.Context) {
	id := c.Query("ID")
	userID := utils.GetUserID(c)
	if err := bsProductInfoService.DeleteBsProductInfo(id, userID); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteBsProductInfoByIds 批量删除二手商品
// @Tags BsProductInfo
// @Summary 批量删除二手商品
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除二手商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /bsProductInfo/deleteBsProductInfoByIds [delete]
func (bsProductInfoApi *BsProductInfoApi) DeleteBsProductInfoByIds(c *gin.Context) {
	ids := c.QueryArray("ids[]")
	userID := utils.GetUserID(c)
	if err := bsProductInfoService.DeleteBsProductInfoByIds(ids, userID); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateBsProductInfo 更新二手商品
// @Tags BsProductInfo
// @Summary 更新二手商品
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body product.BsProductInfo true "更新二手商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /bsProductInfo/updateBsProductInfo [put]
func (bsProductInfoApi *BsProductInfoApi) UpdateBsProductInfo(c *gin.Context) {
	var bsProductInfo product.BsProductInfo
	err := c.ShouldBindJSON(&bsProductInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	bsProductInfo.UpdatedBy = utils.GetUserID(c)
	if bsProductInfo.ID == 0 {
		response.FailWithMessage("请传入id", c)
		return
	}
	if err := bsProductInfoService.UpdateBsProductInfo(bsProductInfo); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindBsProductInfo 用id查询二手商品
// @Tags BsProductInfo
// @Summary 用id查询二手商品
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query product.BsProductInfo true "用id查询二手商品"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /bsProductInfo/findBsProductInfo [get]
func (bsProductInfoApi *BsProductInfoApi) FindBsProductInfo(c *gin.Context) {
	id := c.Query("ID")
	if rebsProductInfo, err := bsProductInfoService.GetBsProductInfo(id); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"rebsProductInfo": rebsProductInfo}, c)
	}
}

// GetBsProductInfoList 分页获取二手商品列表
// @Tags BsProductInfo
// @Summary 分页获取二手商品列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query productReq.BsProductInfoSearch true "分页获取二手商品列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /bsProductInfo/getBsProductInfoList [get]
func (bsProductInfoApi *BsProductInfoApi) GetBsProductInfoList(c *gin.Context) {
	var pageInfo productReq.BsProductInfoSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := bsProductInfoService.GetBsProductInfoInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
